/*
 * 忘记密码
 * page忘记密码
 */
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/user.dart';

class ForgetPsw extends StatefulWidget {
  ForgetPsw({Key key}) : super(key: key);

  @override
  _ForgetPswState createState() => _ForgetPswState();
}

class _ForgetPswState extends State<ForgetPsw> {
  // 获取验证码文字
  String _getCodeText = "获取验证码";
  // 验证码倒计时时间
  int _codeDate = 59;
  // 倒计时控制器
  Timer _codeTimer;

  // ipt手机号码输入控制器
  TextEditingController iptPhoneController;
  // ipt验证码控制器
  TextEditingController iptPINController;
  // ipt登录密码控制器
  TextEditingController iptPWController;
  // ipt重复密码控制器
  TextEditingController iptRePWController;

// life-------------------------------------------------------------------------

  @override
  void initState() {
    super.initState();
    iptPhoneController = TextEditingController();
    iptPINController = TextEditingController();
    iptPWController = TextEditingController();
    iptRePWController = TextEditingController();
  }

  @override
  void dispose() {
    _codeTimer?.cancel();
    _codeTimer = null;
    super.dispose();
  }

// methods----------------------------------------------------------------------

  // back
  void _back() {
    Navigator.of(context).pop();
  }

  // 获取验证码方法
  _getCodeAction() async {
    if (_codeTimer != null) {
      // 冷却中
      return null;
    }
    String phone = iptPhoneController.text;
    var result = await UserApis.getPIN(phone, context);
    if (result != null && result['status'] == 1) {
      debugPrint(result.toString());
      showToast('验证码已发送,请注意查收');
      _codeTimer = Timer.periodic(Duration(seconds: 1), (timer) {
        setState(() {
          if (_codeDate > 0) {
            _getCodeText = '${_codeDate--}秒重新获取';
          } else {
            _getCodeText = '获取验证码';
            _codeDate = 59;
            _codeTimer.cancel();
            _codeTimer = null;
          }
        });
      });
    }
  }

  // 提交修改后的密码
  void _editpassword() async {
    String phone = iptPhoneController.text;
    String pin = iptPINController.text;
    String pw = iptPWController.text;
    String rePw = iptRePWController.text;
    var result = await UserApis.editpassword(phone, pin, pw, rePw);
    if (result != null && result['status'] == 1) {
      showToast('修改密码成功,请重新登录');
      state(context, "user").logout();
    }
  }

// widget-----------------------------------------------------------------------

  /// 背景图
  Widget BG() {
    return Container(
      width: w100(context),
      height: h100(context),
      child: Image.asset(
        'assets/images/homeBg1.png',
        fit: BoxFit.cover,
      ),
    );
  }

  /// 返回
  Widget back() {
    return Container(
      margin: EdgeInsets.only(top: px(35)),
      width: px(150),
      height:px(150),
      child: IconButton(
          onPressed: _back,
          icon: Container(
            alignment: Alignment.topLeft,
            margin: EdgeInsets.only(left: px(40)),
            child: Icon(
              Icons.arrow_back_ios,
              color: Colors.white,
              size: px(36),
            ),
          )),
    );
  }

  // 数字输入框构造器
  Widget _inputNumberBuilder(
      String labelText, TextEditingController controller) {
    return Container(
      margin: EdgeInsets.only(top: px(30)),
      child: TextField(
        controller: controller,
        keyboardType: TextInputType.phone,
        inputFormatters: [
          WhitelistingTextInputFormatter.digitsOnly,
          LengthLimitingTextInputFormatter(11),
        ],
        style: TextStyle(color: col("ffffff")),
        decoration: InputDecoration(
          labelText: labelText,
          labelStyle: TextStyle(color: col("d1a3d7")),
          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: col('d1a3d7')),
          ),
          focusedBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.cyan),
          ),
        ),
      ),
    );
  }

  // 带验证码按钮的输入框
  Widget _inputPINBuilder(String labelText, TextEditingController controller) {
    return Container(
        margin: EdgeInsets.only(top: px(30)),
        child: Stack(
          alignment: AlignmentDirectional.centerEnd,
          children: <Widget>[
            TextField(
              controller: controller,
              keyboardType: TextInputType.phone,
              inputFormatters: [
                WhitelistingTextInputFormatter.digitsOnly,
                LengthLimitingTextInputFormatter(8),
              ],
              style: TextStyle(color: col("ffffff")),
              decoration: InputDecoration(
                labelText: labelText,
                labelStyle: TextStyle(color: col("d1a3d7")),
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: col('d1a3d7')),
                ),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.cyan),
                ),
              ),
            ),
            Container(
                height: px(60),
                child: Stack(
                  children: <Widget>[
                    FlatButton(
                      onPressed: _getCodeAction,
                      color: _codeTimer == null
                          ? col('6f53ff')
                          : Colors.transparent,
                      padding: EdgeInsets.symmetric(horizontal: px(30)),
                      shape: RoundedRectangleBorder(
                        side: _codeTimer == null
                            ? BorderSide.none
                            : BorderSide(
                                color: Colors.white,
                                style: BorderStyle.solid,
                                width: px(1),
                              ),
                        borderRadius: BorderRadius.circular(
                          sp(40),
                        ),
                      ),
                      child: Text(
                        _getCodeText,
                        style: TextStyle(
                          color: col("ffffff"),
                          fontSize: px(26),
                          height: 1.2,
                        ),
                      ),
                    ),
//                FlatButton(
//                  onPressed: _getCodeAction,
//                  color: col("6f53ff"),
//                  padding: EdgeInsets.symmetric(horizontal: px(30)),
//                  shape: RoundedRectangleBorder(
//                    borderRadius: BorderRadius.circular(
//                      sp(40),
//                    ),
//                  ),
//                  child: Text(
//                    _getCodeText,
//                    style: TextStyle(
//                      color: col("ffffff"),
//                      fontSize: px(26),
//                      height: 1.2,
//                    ),
//                  ),
//                ),
                  ],
                )),
          ],
        ));
  }

  // 密码输入框构造函数
  Widget _inputPWBuilder(String labelText, TextEditingController controller) {
    return Container(
      margin: EdgeInsets.only(top: px(30)),
      child: TextField(
        controller: controller,
        obscureText: true,
        style: TextStyle(color: col("ffffff")),
        decoration: InputDecoration(
          labelText: labelText,
          labelStyle: TextStyle(color: col("d1a3d7")),
          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: col('d1a3d7')),
          ),
          focusedBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.cyan),
          ),
        ),
      ),
    );
  }

  /// 服务协议
  Widget _agreement() {
    return Container(
      margin: EdgeInsets.only(top: px(90)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            "确认登录即代表已阅读并同意",
            style: TextStyle(
              fontSize: px(22),
              color: col("ffffff"),
            ),
          ),
          FlatButton(
            onPressed: () => {},
            padding: EdgeInsets.zero,
            child: Text(
              "《软件服务协议》",
              style: TextStyle(
                fontSize: px(22),
                color: col("ffffff"),
                fontWeight: FontWeight.bold,
              ),
            ),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          BG(),
          SafeArea(
            child: ListView(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    back()
                  ],
                ),
                Container(
                  padding: EdgeInsets.only(
                      bottom: px(50), left: px(60), right: px(60)),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(top: px(40)),
                        child: Text(
                          "修改登录密码",
                          style: TextStyle(
                            color: col("ffffff"),
                            fontSize: px(50),
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(top: px(40), bottom: px(50)),
                        child: Text(
                          "填写信息重置密码",
                          style: TextStyle(
                            color: col("ede3f8"),
                            fontSize: px(28),
                          ),
                        ),
                      ),
                      _inputNumberBuilder('手机号码', iptPhoneController),
                      _inputPINBuilder("验证码", iptPINController),
                      _inputPWBuilder("登录密码", iptPWController),
                      _inputPWBuilder("重复密码", iptRePWController),
                      Container(
                        width: px(630),
                        height: px(94),
                        margin: EdgeInsets.only(top: px(130)),
                        child: FlatButton(
                          onPressed: _editpassword,
                          color: col("ffffff"),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(
                              sp(47),
                            ),
                          ),
                          child: Text(
                            "确认",
                            style: TextStyle(
                              color: col('6f53ff'),
                              fontSize: px(36),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),

                /// 服务协议
                _agreement(),
              ],
            ),
          )
        ],
      ),
    );
  }
}
